import os
from pathlib import Path


def create_project_structure():
    # 定义项目根目录
    base_dir = Path(".")

    # 创建后端目录结构
    backend = base_dir / "backend"
    backend_dirs = [
        "crud",
        "routers",
        "services",
        "utils"
    ]

    # 创建前端目录结构
    frontend = base_dir / "frontend"
    frontend_dirs = [
        "pages/auth",
        "pages/library",
        "pages/book",
        "pages/borrow",
        "pages/admin",
        "components",
        "assets"
    ]

    # 创建其他目录
    scripts_dir = base_dir / "scripts"
    tests_dir = base_dir / "tests"

    # 创建所有目录
    all_dirs = [
        backend,
        *[backend / d for d in backend_dirs],
        frontend,
        *[frontend / d for d in frontend_dirs],
        scripts_dir,
        tests_dir
    ]

    for directory in all_dirs:
        directory.mkdir(parents=True, exist_ok=True)
        print(f"创建目录: {directory}")

    # 创建后端文件
    backend_files = [
        "main.py",
        "database.py",
        "models.py",
        "schemas.py",
        "dependencies.py",
        "crud/user_crud.py",
        "crud/library_crud.py",
        "crud/book_crud.py",
        "crud/borrow_crud.py",
        "routers/auth_router.py",
        "routers/admin_router.py",
        "routers/library_router.py",
        "routers/book_router.py",
        "routers/borrow_router.py",
        "services/isbn_service.py",
        "services/borrow_service.py",
        "services/invite_service.py",
        "utils/security.py",
        "utils/config.py",
        "utils/api_client.py"
    ]

    for file in backend_files:
        file_path = backend / file
        file_path.touch()
        print(f"创建文件: {file_path}")

    # 创建前端文件
    frontend_files = [
        "main.py",
        "app.py",
        "pages/auth/login_page.py",
        "pages/auth/register_page.py",
        "pages/home_page.py",
        "pages/library/library_home.py",
        "pages/library/create_library.py",
        "pages/library/manage_library.py",
        "pages/book/add_book.py",
        "pages/book/search_books.py",
        "pages/book/book_detail.py",
        "pages/borrow/borrow_request.py",
        "pages/borrow/manage_requests.py",
        "pages/borrow/return_book.py",
        "pages/admin/user_management.py",
        "pages/admin/library_management.py",
        "components/book_card.py",
        "components/header.py",
        "components/footer.py",
        "components/stats_chart.py"
    ]

    for file in frontend_files:
        file_path = frontend / file
        file_path.touch()
        print(f"创建文件: {file_path}")

    # 创建脚本文件
    script_files = [
        "init_db.py",
        "create_project.py"
    ]

    for file in script_files:
        file_path = scripts_dir / file
        file_path.touch()
        print(f"创建文件: {file_path}")

    # 创建测试文件
    test_files = [
        "test_models.py",
        "test_routers.py"
    ]

    for file in test_files:
        file_path = tests_dir / file
        file_path.touch()
        print(f"创建文件: {file_path}")

    # 创建根目录文件
    root_files = [
        "requirements.txt",
        "README.md"
    ]

    for file in root_files:
        file_path = base_dir / file
        file_path.touch()
        print(f"创建文件: {file_path}")

    # 创建初始化脚本内容
    init_db_content = """from backend.database import Base, engine
from backend.models import User, Library
from backend.utils.security import get_password_hash

def init_database():
    # 创建所有表
    Base.metadata.create_all(bind=engine)

    # 获取数据库会话
    from backend.database import SessionLocal
    db = SessionLocal()

    try:
        # 创建管理员
        admin = User(
            username="ttest",
            hashed_password=get_password_hash("we@test2025"),
            full_name="管理员",
            is_admin=True
        )
        db.add(admin)
        db.commit()

        # 创建示例书阁
        library = Library(
            name="六年级一班共享书阁",
            owner_id=admin.id,
            is_active=True
        )
        db.add(library)
        db.commit()

        # 添加管理员到书阁
        from backend.models import LibraryMember
        member = LibraryMember(
            user_id=admin.id,
            library_id=library.id,
            is_owner=True
        )
        db.add(member)
        db.commit()

        print("数据库初始化完成！")
    except Exception as e:
        db.rollback()
        print(f"初始化失败: {str(e)}")
    finally:
        db.close()

if __name__ == "__main__":
    init_database()
"""

    # 写入初始化脚本
    init_db_path = scripts_dir / "init_db.py"
    with open(init_db_path, "w", encoding="utf-8") as f:
        f.write(init_db_content)

    print(f"已写入内容到: {init_db_path}")


if __name__ == "__main__":
    create_project_structure()
    print("\n项目结构创建完成！")
